var previousTab = "#tab-scorecard";

$(document).ready(function(){

        $('#tabs div').hide();
	$('#tabs div:first').show();
        $('#tabs div:first').children().show();
      
        $(".loadingScreenTabContainer").hide();
	$('#tabs ul li:first').addClass('active');
           //  $('#tabs ul li a:first').children().show();
	$('#tabs ul li a').click(function(){
		$('#tabs ul li').removeClass('active');
		$(this).parent().addClass('active');
		var currentTab = $(this).attr('href');
                longPollingSwitcher(previousTab,currentTab);


                previousTab = currentTab;
             
		$('#tabs div').hide();
		$(currentTab).show();
                $(currentTab).children().show();
                $(currentTab).children().children().show();
                $(currentTab).children().children().children().show();
                $(currentTab).children().children().children().children().show();
		return false;
	});
});


function longPollingSwitcher(previousTab,currentTab)
{
switch(currentTab)
{
case "#tab-scorecard":
killLongPollingConnection(previousTab);
waitForScoreCardStats();
  break;
case "#tab-matchstats":
    killLongPollingConnection(previousTab);
     waitForMatchStats();
  break;
  case "#tab-liveview":
 killLongPollingConnection(previousTab);
 waitForLiveView();
  $("#cricketh_pitch").show();
  $("#baseCanvas").show();

  break;
    case "#tab-fanzone":
 killLongPollingConnection(previousTab);
  break;
default:
break;
}


}

function killLongPollingConnection(tabSelected)
{
switch(tabSelected)
{
case "#tab-scorecard":
  if (isxhrScoreCardActive)
    {
       xhrScoreCard.abort();
    }
  break;
case "#tab-matchstats":

  if (isxhrMatchStatsActive)
     {
     xhrMatchStats.abort();
     }
  break;
  case "#tab-liveview":

  if (isxhrLiveViewActive)
     {
     xhrLiveView.abort();
     }
  break;
default:
break;
}
}